﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LibCom
{
    public enum Treasure { Start01, Start02, Start03, Start04, sym01, sym02, sym03, sym04, sym05, sym06, sym07, sym08, sym09, sym10, sym11, sym12, sym13, sym14, sym15, sym16, sym17, sym18, sym19, sym20, sym21, sym22, sym23, sym24, none  };

    [StructLayout(LayoutKind.Sequential)]
    public struct Card
    {
        public int up;
        public int down;
        public int left;
        public int right;
        public Treasure treasure;
        public int contains_me;
        public int contains_second;
        public int contains_third;
        public int contains_fourth;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ret_Turn
    {
        public int axis;
        public int dir;
        public int pos;
        public int orientation;
        public int go_x;
        public int go_y;
    }

    public struct Zug
    {
        public cardType CardToInsert;
        public int[] insert;
        public int[] moveTo;
    }

    public static class EnumsAndStructs
    {
        public static Card convertCard(cardType c)
        {
            Card ret = new Card();
            int cnt = Players.Length;
            ret.up = c.openings.top ? 1 : 0;
            ret.down = c.openings.bottom ? 1 : 0;
            ret.left = c.openings.left ? 1 : 0;
            ret.right = c.openings.right ? 1 : 0;
            ret.treasure = convertTreasure(c.treasure);
            if (cnt >= 1)
            {
                ret.contains_me = c.pin.Contains(Players[0]) ? 1 : 0;
                if (cnt >= 2)
                {
                    ret.contains_second = c.pin.Contains(Players[1]) ? 1 : 0;
                    if (cnt >= 3)
                    {
                        ret.contains_third = c.pin.Contains(Players[2]) ? 1 : 0;
                        if (cnt >= 4)
                        ret.contains_fourth = c.pin.Contains(Players[3]) ? 1 : 0;
                    }
                }
            }
            return ret;
        }

        public static Treasure convertTreasure(treasureType? t)
        {
            return t.HasValue ? (Treasure)t : Treasure.none;
        }

        public static Zug convertZug(ret_Turn rt, cardType crd)
        {
            Zug ret= new Zug();
            EmptyAndTurnCard(crd, rt.orientation);
            ret.CardToInsert = crd;
            ret.insert = new int[2];
            ret.insert[0] = (rt.axis == 0) ? 2 * rt.pos + 1 : (rt.dir == 0 ? 1 : 0) * (6);
            ret.insert[1] = (rt.axis == 0) ? (rt.dir == 0 ? 1 : 0) * (6) : 2 * rt.pos + 1;
            ret.moveTo = new int[2];
            ret.moveTo[0] = rt.go_y;
            ret.moveTo[1] = rt.go_x;
            return ret;
        }

        public static void EmptyAndTurnCard(cardType crd, int n)
        {
            crd.pin=new int[0];
            for ( int i=0; i<n; i++)
            {
                	bool tmp= crd.openings.top;
	                crd.openings.top= crd.openings.left;
	                crd.openings.left=crd.openings.bottom;
	                crd.openings.bottom=crd.openings.right;
	                crd.openings.right=tmp;
            }
        }

        private static int[] Players;

        public static void setPlayers(int[] p)
        {
            Players = p;
        }

    }
}
